<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="问卷编号" prop="questionnaireNo">
        <el-input
          v-model="queryParams.questionnaireNo"
          placeholder="请输入问卷编号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="问卷标题" prop="questionnaireTitle">
        <el-input
          v-model="queryParams.questionnaireTitle"
          placeholder="请输入问卷标题"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择状态" clearable size="small">
          <el-option
            v-for="dict in statusOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['research:questionnaire:add']"
        >新增
        </el-button>
      </el-col>
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="success"-->
<!--          plain-->
<!--          icon="el-icon-edit"-->
<!--          size="mini"-->
<!--          :disabled="single"-->
<!--          @click="handleUpdate"-->
<!--          v-hasPermi="['research:questionnaire:edit']"-->
<!--        >修改-->
<!--        </el-button>-->
<!--      </el-col>-->
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['research:questionnaire:remove']"
        >删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="info"
          plain
          icon="el-icon-upload2"
          size="mini"
          @click="handleImportQuestion"
          v-hasPermi="['research:questionnaire:question:import']"
        >导入题库
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="info"
          plain
          icon="el-icon-upload2"
          size="mini"
          @click="handleImportConsumer"
          v-hasPermi="[('research:questionnaire:consumer:import')]"
        >导入客户
        </el-button>
      </el-col>
      <!--      <el-col :span="1.5">-->
      <!--        <el-button-->
      <!--          type="warning"-->
      <!--          plain-->
      <!--          icon="el-icon-download"-->
      <!--          size="mini"-->
      <!--          @click="handleExport"-->
      <!--          v-hasPermi="['research:questionnaire:export']"-->
      <!--        >导出-->
      <!--        </el-button>-->
      <!--      </el-col>-->
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="questionnaireList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="序号" type="index" align="center" show-overflow-tooltip width="50px">
      </el-table-column>
      <el-table-column label="问卷编号" align="center" prop="questionnaireNo"/>
      <el-table-column label="问卷标题" align="center" prop="questionnaireTitle"/>
      <el-table-column label="问卷描述" align="center" prop="questionnaireContent"/>
      <el-table-column label="开始时间" align="center" prop="startTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.startTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="结束时间" align="center" prop="endTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.endTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="已发送人数" align="center" prop="sendTotal"/>
      <el-table-column label="已提交人数" align="center" prop="commitTotal"/>
      <el-table-column label="状态" align="center">
        <template slot-scope="scope">
          <el-switch
            v-model="scope.row.status"
            active-value="0"
            inactive-value="1"
            @change="handleStatusChange(scope.row)"
          ></el-switch>
        </template>
      </el-table-column>
      <!--      <el-table-column label="备注" align="center" prop="remark"/>-->
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-detail"
            @click="handleQuestions(scope.row)"
            v-hasPermi="['research:questionnaire:question:list']"
          >查看问卷
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-detail"
            @click="handleConsumers(scope.row)"
            v-hasPermi="['research:questionnaire:consumer:list']"
          >查看客户
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-detail"
            @click="handleAnalysisExport(scope.row)"
          >导出分析数据
          </el-button>
          <el-button
            v-if="Date.parse(scope.row.startTime) > new Date()"
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['research:questionnaire:edit']"
          >修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['research:questionnaire:remove']"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改调研问卷对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="问卷编号" prop="questionnaireNo">
          <el-input v-model="form.questionnaireNo" placeholder="请输入问卷编号"/>
        </el-form-item>
        <el-form-item label="问卷标题" prop="questionnaireTitle">
          <el-input v-model="form.questionnaireTitle" placeholder="请输入问卷标题"/>
        </el-form-item>
        <el-form-item label="问卷描述">
          <el-input v-model="form.questionnaireContent" placeholder="请输入问卷描述"/>
        </el-form-item>
        <el-form-item label="开始时间" prop="startTime">
          <el-date-picker clearable size="small"
                          v-model="form.startTime"
                          type="datetime"
                          value-format="yyyy-MM-dd HH:mm:ss"
                          placeholder="选择开始时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="结束时间" prop="endTime">
          <el-date-picker clearable size="small"
                          v-model="form.endTime"
                          type="datetime"
                          value-format="yyyy-MM-dd HH:mm:ss"
                          placeholder="选择结束时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="状态">
          <el-radio-group v-model="form.status">
            <el-radio
              v-for="dict in statusOptions"
              :key="dict.dictValue"
              :label="dict.dictValue"
            >{{ dict.dictLabel }}
            </el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 问卷问题列表 -->
    <el-drawer
      title="问卷详情"
      :visible.sync="dialogQuestionTableVisible"
      direction="rtl"
      :before-close="handleClose">
      <el-form
        ref="answerForm"
        label-position="left"
        label-width="25px"
      >
        <!-- 循环后端给你的所有题 -->
        <div
          v-for="(item, index) in questionList"
          :key="item.questionId"
        >
          <!-- 1 单选 -->
          <!-- 2 判断 -->
          <!-- 3 多选 -->
          <!-- 4 简答 -->
          <div style="margin-left: 25px" >

            <!-- 题目的信息 -->

            <span style="font-weight: 700">
              第{{ index + 1 }}题：{{
                item.questionContent
              }}
              <span v-if="item.questionType === 1">（单选）</span>
              <span v-if="item.questionType === 2">（判断）</span>
              <span v-if="item.questionType === 3">（多选）</span>
              <span v-if="item.questionType === 4">（简答）</span>
            </span>
          </div>

          <!-- 如果questionType 等于1 那么他是单选题 -->
          <!-- 题目绑定的值是 answerForm.answers[index]  -->

          <div v-if="item.questionType === 1" style="margin-top: 10px">
            <el-form-item label="" prop="answers">
              <el-radio-group disabled v-model="answerForm.answers[index]">
                <div v-for="choiceItem in item.choiceList">
                  <el-radio :label="choiceItem.choiceId" :key="choiceItem.choiceId">
                    {{ choiceItem.choiceContent }}
                  </el-radio>
                  <br>
                </div>
              </el-radio-group>
            </el-form-item>
          </div>

          <!-- 如果questionType 等于2 那么他是判断题 -->
          <!-- 题目绑定的值是 answerForm.answers[index]  -->

          <div v-if="item.questionType === 2" style="margin-top: 10px">
            <el-form-item label="" prop="answers">
              <el-radio-group v-model="answerForm.answers[index]">
                <el-radio label="对">{{ item.stSelecta }}</el-radio>
                <el-radio label="错">{{ item.stSelectb }}</el-radio>
              </el-radio-group>
            </el-form-item>
          </div>

          <!-- 如果questionType 等于3 那么他是多选题 -->
          <!-- 题目绑定的值是 answerForm.answers[index]  -->

          <div v-if="item.questionType === 3">
            <el-form-item label="" prop="answers">
              <el-checkbox-group
                @input="change($event)"
                v-model="answerForm.answers[index]"
                disabled
              >
                <div  v-for="choiceItem in item.choiceList" style="height: 20px">
                  <el-checkbox  :label="choiceItem.choiceId" :key="choiceItem.choiceId">
                    {{ choiceItem.choiceContent }}
                  </el-checkbox>
                  <br>
                </div>
              </el-checkbox-group>
            </el-form-item>
          </div>

          <!-- 如果questionType 等于4 那么他是简答题 -->
          <!-- 题目绑定的值是 answerForm.answers[index]  -->

          <div v-if="item.questionType === 4" style="margin-top: 10px">
            <el-form-item label="" prop="answers">
              <!-- <el-form-item label="活动形式"> -->
              <el-input disabled
                        type="textarea"
                        v-model="answerForm.answers[index]"
              ></el-input>
            </el-form-item>
          </div>
        </div>

      </el-form>
    </el-drawer>

    <!-- 问卷问题列表 -->
<!--    <el-dialog v-loading="questionLoading" width="500px" title="问卷" :visible.sync="dialogQuestionTableVisible">-->
<!--      <el-form-->
<!--        ref="answerForm"-->
<!--        label-width="50px"-->
<!--      >-->
<!--        &lt;!&ndash; 循环后端给你的所有题 &ndash;&gt;-->
<!--        <div-->
<!--          v-for="(item, index) in questionList"-->
<!--          :key="item.questionId"-->
<!--        >-->
<!--          &lt;!&ndash; 1 单选 &ndash;&gt;-->
<!--          &lt;!&ndash; 2 判断 &ndash;&gt;-->
<!--          &lt;!&ndash; 3 多选 &ndash;&gt;-->
<!--          &lt;!&ndash; 4 简答 &ndash;&gt;-->
<!--          <div class="VSTD_box_item_title">-->

<!--            &lt;!&ndash; 题目的信息 &ndash;&gt;-->

<!--            <p style="font-weight: 700">-->
<!--              第{{ index + 1 }}题：{{-->
<!--                item.questionContent-->
<!--              }}-->
<!--              <span v-if="item.questionType === 1">（单选）</span>-->
<!--              <span v-if="item.questionType === 2">（判断）</span>-->
<!--              <span v-if="item.questionType === 3">（多选）</span>-->
<!--              <span v-if="item.questionType === 4">（简答）</span>-->
<!--            </p>-->
<!--          </div>-->

<!--          &lt;!&ndash; 如果questionType 等于1 那么他是单选题 &ndash;&gt;-->
<!--          &lt;!&ndash; 题目绑定的值是 answerForm.answers[index]  &ndash;&gt;-->

<!--          <div v-if="item.questionType === 1" class="VSTD_box_item_select">-->
<!--            <el-form-item label="" prop="answers">-->
<!--              <el-radio-group v-model="answerForm.answers[index]">-->
<!--                <div v-for="choiceItem in item.choiceList">-->
<!--                  <el-radio disabled :label="choiceItem.choiceId" :key="choiceItem.choiceId">-->
<!--                    {{ choiceItem.choiceContent }}-->
<!--                  </el-radio>-->
<!--                  <br>-->
<!--                </div>-->
<!--              </el-radio-group>-->
<!--            </el-form-item>-->
<!--          </div>-->

<!--          &lt;!&ndash; 如果questionType 等于2 那么他是判断题 &ndash;&gt;-->
<!--          &lt;!&ndash; 题目绑定的值是 answerForm.answers[index]  &ndash;&gt;-->

<!--          <div v-if="item.questionType === 2" class="VSTD_box_item_select">-->
<!--            <el-form-item label="" prop="answers">-->
<!--              <el-radio-group v-model="answerForm.answers[index]">-->
<!--                <el-radio label="对">{{ item.stSelecta }}</el-radio>-->
<!--                <el-radio label="错">{{ item.stSelectb }}</el-radio>-->
<!--              </el-radio-group>-->
<!--            </el-form-item>-->
<!--          </div>-->

<!--          &lt;!&ndash; 如果questionType 等于3 那么他是多选题 &ndash;&gt;-->
<!--          &lt;!&ndash; 题目绑定的值是 answerForm.answers[index]  &ndash;&gt;-->

<!--          <div v-if="item.questionType === 3" class="VSTD_box_item_select">-->
<!--            <el-form-item label="" prop="answers">-->
<!--              <el-checkbox-group-->
<!--                @input="change($event)"-->
<!--                v-model="answerForm.answers[index]"-->
<!--              >-->
<!--                <div v-for="choiceItem in item.choiceList">-->
<!--                  <el-checkbox disabled :label="choiceItem.choiceId" :key="choiceItem.choiceId">-->
<!--                    {{ choiceItem.choiceContent }}-->
<!--                  </el-checkbox>-->
<!--                  <br>-->
<!--                </div>-->
<!--              </el-checkbox-group>-->
<!--            </el-form-item>-->
<!--          </div>-->

<!--          &lt;!&ndash; 如果questionType 等于4 那么他是简答题 &ndash;&gt;-->
<!--          &lt;!&ndash; 题目绑定的值是 answerForm.answers[index]  &ndash;&gt;-->

<!--          <div v-if="item.questionType === 4" class="VSTD_box_item_select">-->
<!--            <el-form-item label="" prop="answers">-->
<!--              &lt;!&ndash; <el-form-item label="活动形式"> &ndash;&gt;-->
<!--              <el-input disabled-->
<!--                        type="textarea"-->
<!--                        v-model="answerForm.answers[index]"-->
<!--              ></el-input>-->
<!--            </el-form-item>-->
<!--          </div>-->
<!--        </div>-->

<!--      </el-form>-->
<!--    </el-dialog>-->

    <!-- 问卷客户列表-->
    <el-dialog v-loading="consumerLoading" title="客户列表" :visible.sync="dialogConsumerTableVisible">
      <el-table :data="consumerList">
        <el-table-column label="序号" type="index" align="center" show-overflow-tooltip width="50px">
        </el-table-column>
        <el-table-column label="客户姓名" align="center" prop="consumerName"/>
        <el-table-column label="电话号码" align="center" prop="mobile"/>
        <el-table-column label="性别" align="center" prop="sex" :formatter="sexFormat"/>
        <el-table-column label="省份" align="center" prop="province"/>
        <el-table-column label="城市" align="center" prop="city"/>
        <el-table-column label="生日" align="center" prop="birthday" width="180">
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.birthday, '{y}-{m}-{d}') }}</span>
          </template>
        </el-table-column>
        <el-table-column label="婚姻状况" align="center" prop="maritalStatus" :formatter="maritalStatusFormat"/>
        <el-table-column label="学历" align="center" prop="education"/>
        <el-table-column label="问卷编号" align="center" prop="questionnaireNo"/>
        <el-table-column label="vin号" align="center" prop="vin"/>
        <el-table-column label="产品编号" align="center" prop="productNo"/>
        <el-table-column label="产品名称" align="center" prop="productName"/>
        <el-table-column label="发动机序列号" align="center" prop="engineNo"/>
        <el-table-column label="生产日期" align="center" prop="productionDate" width="180">
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.productionDate, '{y}-{m}-{d}') }}</span>
          </template>
        </el-table-column>
        <el-table-column label="录入经销商名称" align="center" prop="inputDealerName"/>
        <el-table-column label="经销商名称" align="center" prop="dealerName"/>
        <el-table-column label="经销商类型" align="center" prop="dealerType" :formatter="dealerTypeFormat"/>
        <el-table-column label="购车经历" align="center" prop="carBuyExperience"/>
        <el-table-column label="备注" align="center" prop="remark"/>
      </el-table>

      <pagination
        v-show="consumerTotal>0"
        :total="consumerTotal"
        :page.sync="queryConsumerParams.pageNum"
        :limit.sync="queryConsumerParams.pageSize"
        @pagination="paginationConsumers"
      />
    </el-dialog>

    <!-- 问卷问题导入对话框 -->
    <el-dialog :title="upload.title" :visible.sync="upload.open" width="400px" append-to-body>
      <el-upload
        ref="upload"
        :limit="1"
        accept=".xlsx, .xls"
        :headers="upload.headers"
        :action="upload.url + '?updateSupport=' + upload.updateSupport"
        :disabled="upload.isUploading"
        :on-progress="handleFileUploadProgress"
        :on-success="handleFileSuccess"
        :auto-upload="false"
        drag
      >
        <i class="el-icon-upload"></i>
        <div class="el-upload__text">
          将文件拖到此处，或
          <em>点击上传</em>
        </div>
        <div v-if="upload.title === '题库导入'" class="el-upload__tip" slot="tip">
          <!--          <el-checkbox v-model="upload.updateSupport" />是否更新已经存在的用户数据-->
          <el-link type="info" style="font-size:12px" @click="importQuestionTemplate">下载模板</el-link>
        </div>
        <div v-if="upload.title === '客户导入'" class="el-upload__tip" slot="tip">
          <!--          <el-checkbox v-model="upload.updateSupport" />是否更新已经存在的用户数据-->
          <el-link type="info" style="font-size:12px" @click="importConsumerTemplate">下载模板</el-link>
        </div>
        <div class="el-upload__tip" style="color:red" slot="tip">提示：仅允许导入“xls”或“xlsx”格式文件！</div>
      </el-upload>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitFileForm">确 定</el-button>
        <el-button @click="upload.open = false">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listQuestionnaire,
  getQuestionnaire,
  delQuestionnaire,
  addQuestionnaire,
  updateQuestionnaire,
  exportQuestionnaire,
  researchQuestionnaireQuestionList,
  importQuestionTemplate,
  importConsumerTemplate,
  researchQuestionnaireConsumerList, exportTest, changeQuestionnaireStatus
} from '@/api/research/questionnaire'
import Editor from '@/components/Editor'
import { getToken } from '@/utils/auth'
import { changeJobStatus } from '@/api/monitor/job'

export default {
  name: 'Questionnaire',
  components: {
    Editor
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 客户列表遮罩层
      consumerLoading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 客户总条数
      consumerTotal: 0,
      // 调研问卷表格数据
      questionnaireList: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 状态(0-开启,1-关闭)字典
      statusOptions: [],
      // 导入参数
      upload: {
        // 是否显示弹出层（导入）
        open: false,
        // 弹出层标题（导入）
        title: '',
        // 是否禁用上传
        isUploading: false,
        // 是否更新已经存在的数据
        updateSupport: 0,
        // 设置上传的请求头部
        headers: { Authorization: 'Bearer ' + getToken() }
        // 上传的地址(在操作中赋值)
        // url: process.env.VUE_APP_BASE_API + "/research/questionnaire/question/import"
      },
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        questionnaireNo: null,
        questionnaireTitle: null,
        status: null
      },
      // 客户列表分页查询参数
      queryConsumerParams: {
        pageNum: 1,
        pageSize: 10,
        // 问卷id(存储当前点击的问卷id)
        questionnaireId: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
      // 问卷列表框
      dialogQuestionTableVisible: false,
      // 客户列表框
      dialogConsumerTableVisible: false,
      // 问题列表
      questionList: [],
      // 客户列表
      consumerList: [],
      // 性别字典
      sexOptions: [],
      // 婚姻状况字典
      maritalStatusOptions: [],
      // 经销商类型字典
      dealerTypeOptions: [],
      answerForm: {
        answers: []				//每一道题的值
      }
    }
  },
  created() {
    this.getList()
    this.getDicts('sv_research_status').then(response => {
      this.statusOptions = response.data
    })
  },
  methods: {
    // 性别(0-女,1-男)字典翻译
    sexFormat(row, column) {
      return this.selectDictLabel(this.sexOptions, row.sex)
    },
    // 婚姻状况(0-未婚,1-已婚)字典翻译
    maritalStatusFormat(row, column) {
      return this.selectDictLabel(this.maritalStatusOptions, row.maritalStatus)
    },
    // 经销商类型字典翻译
    dealerTypeFormat(row, column) {
      return this.selectDictLabel(this.dealerTypeOptions, row.dealerType)
    },
    /** 查询调研问卷列表 */
    getList() {
      this.loading = true
      listQuestionnaire(this.queryParams).then(response => {
        this.questionnaireList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    // 状态(0-开启,1-关闭)字典翻译
    statusFormat(row, column) {
      return this.selectDictLabel(this.statusOptions, row.status)
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        questionnaireId: null,
        questionnaireNo: null,
        questionnaireTitle: null,
        questionnaireContent: null,
        startTime: null,
        endTime: null,
        status: '0',
        delFlag: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null
      }
      this.resetForm('form')
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.questionnaireId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      this.title = '添加调研问卷'
    },
    /** 查看问卷问题操作 */
    handleQuestions(row) {
      this.dialogQuestionTableVisible = true
      researchQuestionnaireQuestionList(row.questionnaireId).then(response => {
        this.questionList = response.rows
        for (let i = 0; i < this.questionList.length; i++) {
          //如果他是多选题的话，把它初始化为一个数组，而不是普通的字符串
          if (this.questionList[i].questionType === 3) {
            this.answerForm.answers[i] = []
          } else {
            this.answerForm.answers[i] = ''
          }
        }
      })
    },
    /** 查看客户列表操作 */
    handleConsumers(row) {
      this.consumerLoading = true
      this.queryConsumerParams.questionnaireId = row.questionnaireId
      this.dialogConsumerTableVisible = true
      researchQuestionnaireConsumerList(this.queryConsumerParams).then(response => {
        this.consumerList = response.rows
        this.consumerTotal = response.total
        this.consumerLoading = false
      })

    },
    /** 导出分析数据按钮操作 */
    handleAnalysisExport(row) {
      this.$confirm('是否确认导出分析数据?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(function() {
        return exportTest(row.questionnaireId)
      }).then(response => {
        this.download(response.msg)
      })
    },
    /** 客户列表分页操作 */
    paginationConsumers() {
      this.loading = true
      this.dialogConsumerTableVisible = true
      researchQuestionnaireConsumerList(this.queryConsumerParams).then(response => {
        this.consumerList = response.rows
        this.consumerTotal = response.total
        this.loading = false
      })
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      const questionnaireId = row.questionnaireId || this.ids
      getQuestionnaire(questionnaireId).then(response => {
        this.form = response.data
        this.open = true
        this.title = '修改调研问卷'
      })
    },
    // 状态修改
    handleStatusChange(row) {
      let text = row.status === "0" ? "启用" : "停用";
      this.$confirm('确认要' + text  + row.questionnaireTitle + '问卷吗?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(function() {
        return changeQuestionnaireStatus(row.questionnaireId, row.status);
      }).then(() => {
        this.msgSuccess(text + "成功");
      }).catch(function() {
        row.status = row.status === "0" ? "1" : "0";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          if (this.form.questionnaireId != null) {
            updateQuestionnaire(this.form).then(response => {
              this.msgSuccess('修改成功')
              this.open = false
              this.getList()
            })
          } else {
            addQuestionnaire(this.form).then(response => {
              this.msgSuccess('新增成功')
              this.open = false
              this.getList()
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const questionnaireIds = row.questionnaireId || this.ids
      this.$confirm('是否确认删除调研问卷编号为"' + questionnaireIds + '"的数据项?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(function() {
        return delQuestionnaire(questionnaireIds)
      }).then(() => {
        this.getList()
        this.msgSuccess('删除成功')
      })
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams
      this.$confirm('是否确认导出所有调研问卷数据项?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(function() {
        return exportQuestionnaire(queryParams)
      }).then(response => {
        this.download(response.msg)
      })
    },
    /** 导入题库按钮操作 */
    handleImportQuestion() {
      this.upload.title = '题库导入'
      this.upload.url = process.env.VUE_APP_BASE_API + '/research/questionnaire/question/import'
      this.upload.open = true
    },
    /** 下载题库导入模板操作 */
    importQuestionTemplate() {
      importQuestionTemplate().then(response => {
        this.download(response.msg)
      })
    },
    /** 导入客户按钮操作 */
    handleImportConsumer() {
      this.upload.title = '客户导入'
      this.upload.url = process.env.VUE_APP_BASE_API + '/research/questionnaire/consumer/import'
      this.upload.open = true
    },
    /** 下载客户导入模板操作 */
    importConsumerTemplate() {
      importConsumerTemplate().then(response => {
        this.download(response.msg)
      })
    },
    // 文件上传中处理
    handleFileUploadProgress(event, file, fileList) {
      this.upload.isUploading = true
    },
    // 文件上传成功处理
    handleFileSuccess(response, file, fileList) {
      this.upload.open = false
      this.upload.isUploading = false
      this.$refs.upload.clearFiles()
      this.$alert(response.msg, '导入结果', { dangerouslyUseHTMLString: true })
      this.getList()
    },
    // 提交上传文件
    submitFileForm() {
      this.$refs.upload.submit()
    },
    //给多选框用，否则会出现一个bug，就是先点一下多选框，然后没反应，
    //然后点一下按钮或者点击下拉框后，这个多选框就高亮了
    change(e) {
      this.$forceUpdate()
    }
  }

}
</script>

<style rel="stylesheet/scss" lang="scss">

// 抽屉滚动条
.el-drawer{

  overflow: scroll
}

</style>
